<?php

class AccountingController extends Zend_Controller_Action
{
    //
    function preDispatch()
    {
		if(!$_SESSION['identity'])
        {
            $this->_redirect("/login");
        }
		
         //Make menu
         $mod_menu = new Db_Tables2_BDictMenu();
         $this->view->main_menu = $mod_menu->MenuDecolation();
        
        $mod_params = new Db_Tables2_BDictParams();
        $this->view->sys_name = $mod_params->GetSysName();
    }
	
    function indexAction()
    {
		$params = $this->_request->getParams();
		
		$this->view->title = "回款计划列表";
		
		$sales_role_id = array();
		
		$mod_params = new Db_Tables2_BDictParams();
		$sales_role_id[] = $mod_params->GetVal("LEVEL-1");
		$sales_role_id[] = $mod_params->GetVal("LEVEL-2");
		
		$mod_user = new Db_Tables2_User();
		$sales = $mod_user->GetUsersByRole($sales_role_id);
		
		if("LEVEL-2" == $_SESSION['identity']['level']) //销售主管只可以看属于自己的员工合同
		{
			$mod_user = new Db_Tables2_User();
			$my_staff_ids = $mod_user->GetMyStaffIds($_SESSION['identity']['userid']);
			
			foreach($sales as $sid => $sname)
			{
				if(!in_array($sid, $my_staff_ids))
				{
					unset($sales[$sid]);
				}
			}
		}
		
		$this->view->sales = $sales;
		
		$mod_products = new Db_Tables2_BDictProduct();
		$this->view->products = $mod_products->GetProducts();
		
		$params_pool = $params_pool2 = $params_pool3 = $params_pool4 = $params_pool5 = array();
		$mod_tables = new Db_Joins_TablesJoins();
    	
		if($params['date_start'] && !$params['period_id'])
    	{
    		$mod_tables->date_start = $params['date_start'];
    		$this->view->date_start = $params['date_start'];
    		$params_pool[] = $params_pool2[] = $params_pool3[] = $params_pool4[] = $params_pool5 = "date_start=".$params['date_start'];
    	}
    	
		if($params['date_end'] && !$params['period_id'])
    	{
    		$mod_tables->date_end = $params['date_end'];
    		$this->view->date_end = $params['date_end'];
    		$params_pool[] = $params_pool2[] = $params_pool3[] = $params_pool4[] = $params_pool5 = "date_end=".$params['date_end'];
    	}
    	
		if($params['period_id'])
    	{
    		$mod_tables->period_id = $params['period_id'];
    		$this->view->period_id = $params['period_id'];
    		$params_pool[] = $params_pool3[] = $params_pool4[] = $params_pool5 = "period_id=".$params['period_id'];
    	}
    	
		if($params['date_start_2'] && !$params['period_id_2'])
    	{
    		$mod_tables->date_start_2 = $params['date_start_2'];
    		$this->view->date_start_2 = $params['date_start_2'];
    		$params_pool[] = $params_pool2[] = $params_pool3[] = $params_pool4[] = $params_pool5 = "date_start_2=".$params['date_start_2'];
    	}
    	
		if($params['date_end_2'] && !$params['period_id_2'])
    	{
    		$mod_tables->date_end_2 = $params['date_end_2'];
    		$this->view->date_end_2 = $params['date_end_2'];
    		$params_pool[] = $params_pool2[] = $params_pool3[] = $params_pool4[] = $params_pool5 = "date_end_2=".$params['date_end_2'];
    	}
    	
		if($params['period_id_2'])
    	{
    		$mod_tables->period_id_2 = $params['period_id_2'];
    		$this->view->period_id_2 = $params['period_id_2'];
    		$params_pool[] = $params_pool3[] = $params_pool4[] = "period_id_2=".$params['period_id_2'];
    	}
		
		if($params['sales_emp_id'] && "LEVEL-1" != $_SESSION['identity']['level'])
    	{
    		$mod_tables->sales_emp_id = $params['sales_emp_id'];
    		$this->view->sales_emp_id = $params['sales_emp_id'];
    		$params_pool[] = $params_pool2[] = $params_pool3[] = $params_pool4[] = $params_pool5 = "sales_emp_id=".$params['sales_emp_id'];
    	}elseif("LEVEL-1" == $_SESSION['identity']['level']) //销售只可以看自己
		{
			$mod_tables->sales_emp_id = $_SESSION['identity']['userid'];
		}
		
		if($params['product_id'])
    	{
    		$mod_tables->product_id = $params['product_id'];
    		$this->view->product_id = $params['product_id'];
    		$params_pool[] = $params_pool2[] = $params_pool3[] = $params_pool5 = "product_id=".$params['product_id'];
    	}
		
		if($params['status_type'])
    	{
			$mod_tables->status_type = $params['status_type'];
    		$this->view->status_type = $params['status_type'];
    		$params_pool[] = $params_pool2[] = $params_pool4[] = $params_pool5 = "status_type=".$params['status_type'];
    	}
		
		if($params['keyword'])
    	{
    		$mod_tables->keyword = $params['keyword'];
    		$this->view->keyword = $params['keyword'];
    		$params_pool[] = $params_pool2[] = $params_pool3[] = $params_pool4[] = $params_pool5 = "keyword=".$params['keyword'];
    	}
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_tables->page_id = $page_id;
    		$this->view->page_id = $page_id;
    	}
		
		$this->view->data = $result = $mod_tables->GetPayPlans();
		
		//make pagination
    	if(!empty($params_pool))
    	{
    		$params_string = implode("&", $params_pool);
    	}else{
    		$params_string = "";
    	}
		
    	if(!empty($params_pool2))
    	{
    		$params_string2 = implode("&", $params_pool2);
    	}else{
    		$params_string2 = "";
    	}
		
    	if(!empty($params_pool3))
    	{
    		$params_string3 = implode("&", $params_pool3);
    	}else{
    		$params_string3 = "";
    	}
		
    	if(!empty($params_pool4))
    	{
    		$params_string4 = implode("&", $params_pool4);
    	}else{
    		$params_string4 = "";
    	}
		
    	if(!empty($params_pool5))
    	{
    		$params_string5 = implode("&", $params_pool5);
    	}else{
    		$params_string5 = "";
    	}
    	
    	if(1 == $page_id)
    	{
    		$first_page_string = "";
    		$previous_page_string = "";
    	}else{
    		$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
    		$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
    	}
    	
    	if($page_id < $result['total_pages'] )
    	{
    		$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
    		$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
    	}else{
    		$next_page_string = "";
    		$last_page_string = "";
    	}
    	
		$this->view->params_string = $params_string;
		$this->view->params_string2 = $params_string2;
		$this->view->params_string3 = $params_string3;
		$this->view->params_string4 = $params_string4;
		$this->view->params_string5 = $params_string5;
    	$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
//        $this->view->export_link = "/pay-plan/index-export?".$params_string;
		
		if($params['msg'])
		{
				switch ($params['msg'])
				{
					case 1:
						$this->view->msg = "您已成功申请合同，请等待审核。";
						break;
					case 2:
						$this->view->msg = "状态更改成功";
						break;
					case 3:
						$this->view->msg = "状态更改失败";
						break;
					case 4:
						$this->view->msg = "关键参数缺失";
						break;
					case 5:
						$this->view->msg = "您无修改此合同的权限";
						break;
					case 6:
						$this->view->msg = "找不到此合同";
						break;
					case 7:
						$this->view->msg = "您已成功编辑合同，请等待审核。";
						break;
					case 8:
						$this->view->msg = "该合同不满足开票标准";
						break;
					case 9:
						$this->view->msg = "必须合同发起人才可以开票";
						break;
					case 10:
						$this->view->msg = "开票由于异常原因导致失败，请联系管理员";
						break;
					case 11:
						$this->view->msg = "开票申请成功，请等待财务执行";
						break;
					case 12:
						$this->view->msg = "找不到此开票申请";
						break;
				}
		}
	}
}